package com.baozhun.mall.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.baozhun.mall.common.R;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

/**
 * @Title：首页三级分类指示器
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/12/26
 * @Version：1.0.0
 */
public class CategoryIndicator extends LinearLayout {
    /**
     * 滚动条
     */
    private View progressView;
    private float currentProgress = 0;
    //每一页展示的icon个数
    private float displayCount = 4;
    //icon的总个数
    private float totalCount = 5;
    //进度条组件总宽度32dp
    private float totalWidth = 35;
    //第二进度的宽度dp
    private float secondWidth = 12;

    public CategoryIndicator(Context context) {
        this(context, null);
    }

    public CategoryIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        // 引入布局
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_category_progress_bar, this);
        progressView = this.findViewById(R.id.progress_view);
        setProgressWidth();
    }

    /**
     * 设置进度条宽度
     */
    private void setProgressWidth() {
//        blue_width_dp = (display_count / total_count) * width_dp;
//        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
//                UIUtil.dip2px(getContext(), blue_width_dp), LayoutParams.MATCH_PARENT);
//        progress_view.setLayoutParams(params);
    }

    /**
     * 设置总页数
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        setProgressWidth();
    }

    /**
     * 更新进度
     */
    public void setProgress(float percentProgress) {
        float progress;
        progress = UIUtil.dip2px(getContext(), totalWidth - secondWidth) * percentProgress;
        // 开启动画
        TranslateAnimation animation = new TranslateAnimation(currentProgress, progress, 0, 0);
        animation.setDuration(200);
        // 设置动画过后不复位
        animation.setFillEnabled(true);
        animation.setFillAfter(true);

        progressView.startAnimation(animation);
        currentProgress = progress;
    }
}
